"""
Projectplace OAuth Support for Google App Engine Apps.

Using this in your app should be relatively straightforward:

* Edit the configuration section with the CONSUMER_KEY and CONSUMER_SECRET
  from Projectplace.

* Modify to reflect your App's domain and set the callback URL on Twitter to:

    http://your-app-name.appspot.com/oauth/twitter/callback

* Use the demo in ``MainHandler`` as a starting guide to implementing your app.

This DEMO application has been built on the foundation of tav@espians.com's work.

Sourcecode for orgininal application can be found here: http://github.com/tav/tweetapp

"""

# Released into the Public Domain by tav@espians.com
import sys


from oauth.config import OAUTH_APP_SETTINGS
from wsgiref.handlers import CGIHandler
from google.appengine.ext.webapp import RequestHandler
from google.appengine.ext.webapp import WSGIApplication
from oauth.client import OAuthClient
from os.path import dirname, join as join_path

sys.path.insert(0, join_path(dirname(__file__), 'lib')) # extend sys.path


class OAuthHandler(RequestHandler):

    def get(self, service, action=''):

        if service not in OAUTH_APP_SETTINGS:
            return self.response.out.write(
                "Unknown OAuth Service Provider: %r" % service
                )
        
        OAuthClient.cleanup()
        client = OAuthClient(service, self)

        if action in client.__public__:
            self.response.out.write(getattr(client, action)())
        else:
            self.response.out.write(client.login())

class MainHandler(RequestHandler):
    
    def get(self):
        from html import StartPage, APITestPage
        
        client = OAuthClient('projectplace', self)
        
        write = self.response.out.write
        
        if not client.get_cookie():
            write(StartPage().to_html())
            return
        
        try:
            write(APITestPage(self).to_html())
        except ValueError:
            client.logout()
            
class APIRequestHandler(RequestHandler):
    
    def get(self):
        from pp_api import ProjectplaceGETRequest
        
        api_path = self.request.get('api_path')
        
        ProjectplaceGETRequest(api_path, self)

    def post(self):
        from pp_api import ProjectplacePOSTRequest
        
        api_path = self.request.get('api_path')
        
        ProjectplacePOSTRequest(api_path, self)
        
    def put(self):
        from pp_api import ProjectplacePUTRequest
        
        api_path = self.request.get('api_path')
        
        ProjectplacePUTRequest(api_path, self)
        
class DocumentDownloadRequestHandler(RequestHandler):
    
    def get(self):
        from pp_api import ProjectplaceDownloadRequest
        
        api_path = self.request.get('api_path')
        version = self.request.get('vid')
        
        ProjectplaceDownloadRequest(api_path, version, self)
        
def main():

    application = WSGIApplication([
       ('/oauth/(.*)/(.*)', OAuthHandler),
       ('/get/*', APIRequestHandler),
       ('/doc_download/*', DocumentDownloadRequestHandler),
       ('/', MainHandler)
       ], debug=True)

    CGIHandler().run(application)

if __name__ == '__main__':
    main()